/**
 * 
 */
package com.sys.console.business.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sys.console.business.SysAuthorityService;
import com.sys.console.cookie.Constant_Cookie;
import com.sys.console.mapper.Sys_AuthorityMapper;
import com.sys.console.pojo.SysAuthorityTree;
import com.sys.console.pojo.Sys_Authority;
import com.sys.console.pojo.Sys_User;
import com.sys.object.common.ObjException;
import com.sys.object.common.ObjResultCode;
import com.sys.object.common.page.PageBean;
import com.sys.object.common.utils.DateUtils;
import com.sys.object.common.utils.IpUtils;

/**
 * 2014-11-05
 * 
 * @author 陈成军 权限标识管理
 */
@Component
public class SysAuthorityServiceImpl implements SysAuthorityService {
	public final Logger delLoger = Logger.getLogger("delLoger");
	public final Logger saveOrupdateLoger = Logger.getLogger("saveOrupdateLoger");
	@Autowired Sys_AuthorityMapper sysAuthorityMapper;

	@Override
	public String selectByMid(Integer mid) throws Exception {
		// TODO Auto-generated method stub
		return sysAuthorityMapper.selectByMid(mid);
	}

	@Override
	public ArrayList<Sys_Authority> selectByList() throws Exception {
		// TODO Auto-generated method stub
		return sysAuthorityMapper.selectByList();
	}

	@Override
	public ArrayList<Sys_Authority> selectListByMid(Integer mid)
			throws Exception {
		// TODO Auto-generated method stub
		return sysAuthorityMapper.selectListByMid(mid);
	}

	@Override
	public PageBean queryPageList(Sys_Authority entity, PageBean page) throws Exception {
		int rowCount = sysAuthorityMapper.selectByPageCount(entity);
		page.setRowCount(rowCount);
		entity.setOffset(page.getStartRow());
		entity.setLimit(page.getPerpage());
		page.setList(sysAuthorityMapper.selectByPage(entity));
		return page;
	}

	@Override
	public Sys_Authority queryByPrimaryKey(Integer authorityid) throws Exception {
		if (authorityid!= null&&authorityid>0)
			return sysAuthorityMapper.selectByPrimaryKey(authorityid);
		return null;
	}

	@Override
	public int saveOrUpdate(Sys_Authority record) throws Exception {
		Sys_User sysUser = (Sys_User) SecurityUtils.getSubject().getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
		if(sysUser==null){
			throw new ObjException(ObjResultCode.USER_SESSION_EXIT_LOGIN);
		}
		if (record != null&&record.getAid()!=null&&record.getAid()>0){
			saveOrupdateLoger.info(DateUtils.foramteDate_hh_mm_ss(new Date())+"	"+IpUtils.getIpAddr()+"	"+sysUser.getUsername()+"	更新	"+record.getName()+"	SysAuthorityServiceImpl.insert");
			return sysAuthorityMapper.updateByPrimaryKey(record);
		}
		saveOrupdateLoger.info(DateUtils.foramteDate_hh_mm_ss(new Date())+"	"+IpUtils.getIpAddr()+"	"+sysUser.getUsername()+"	新加	"+record.getName()+"	SysAuthorityServiceImpl.insert");
		return sysAuthorityMapper.insert(record);
	}

	@Override
	public int remove(Integer authorityid) throws Exception {
		if (authorityid == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);
		Sys_User sysUser = (Sys_User) SecurityUtils.getSubject().getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
		Sys_Authority entity = this.queryByPrimaryKey(authorityid);
		saveOrupdateLoger.info(DateUtils.foramteDate_hh_mm_ss(new Date())+"	"+IpUtils.getIpAddr()+"	"+sysUser.getUsername()+"	删除	"+entity.getName()+"	SysAuthorityServiceImpl.insert");
		return sysAuthorityMapper.deleteByPrimaryKey(authorityid);		
	}

	@Override
	public int delete(Integer authorityid) throws Exception {
		// TODO Auto-generated method stub
		if (authorityid == null)
			throw new ObjException(ObjResultCode.DATA_ERROR);
		Sys_User sysUser = (Sys_User) SecurityUtils.getSubject().getSession().getAttribute(Constant_Cookie.CURRENT_USER_SESSION);
		if(sysUser==null){
			throw new ObjException(ObjResultCode.USER_SESSION_EXIT_LOGIN);
		}
		Sys_Authority entity = this.queryByPrimaryKey(authorityid);
		delLoger.info(DateUtils.foramteDate_hh_mm_ss(new Date())+"	"+IpUtils.getIpAddr()+"	"+sysUser.getUsername()+"	删除	"+entity.getName()+"	SysAuthorityServiceImpl.insert");
		return sysAuthorityMapper.deleteByPrimaryKey(authorityid);		
	}

	@Override
	public int updateStatus(Sys_Authority entity) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateStatus(String status, String[] ids) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<SysAuthorityTree> selectByTree() throws Exception {
		List<SysAuthorityTree> treeList = new ArrayList<SysAuthorityTree>();
		SysAuthorityTree tree = new SysAuthorityTree();
		tree.setId("0");
		tree.setpId("-1");
		tree.setName("根节点");
		List<SysAuthorityTree> list = sysAuthorityMapper.selectByTree();
		tree.setChildren(list);
		treeList.add(tree);
		return treeList;
	}

}
